package com.fayuan.nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fayuan.nocv.entity.Menu;
import com.fayuan.nocv.entity.User;
import com.fayuan.nocv.service.MenuService;
import com.fayuan.nocv.service.RoleService;
import com.fayuan.nocv.utils.TreeNode;
import com.fayuan.nocv.utils.TreeNodeBuilder;
import com.fayuan.nocv.vo.DataView;
import com.fayuan.nocv.vo.MenuVo;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/loadAllMenu")
    @ResponseBody
    public DataView getAllMenu(MenuVo menuVo){

        IPage<Menu>page = new Page<>(menuVo.getPage(),menuVo.getLimit());
        QueryWrapper<Menu>wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("ordernum");
        if (menuVo.getTitle()!=null){
            wrapper.like("title",menuVo.getTitle());
        }
        IPage<Menu>page1 = menuService.page(page,wrapper);
        DataView dataView = new DataView(page1.getTotal(), page1.getRecords());
        return dataView;
    }
    //加载左侧树状结构
    @PostMapping("/loadMenuManagerLeftTreeJson")
    @ResponseBody
    public DataView loadMenuManagerLeftTreeJson(){

        List<Menu>list = menuService.list();
        List<TreeNode>treeNodes = new ArrayList<>();
        for (Menu menu : list) {
           Boolean open = menu.getOpen() == 1?true:false;
           treeNodes.add(new TreeNode(menu.getId(),menu.getPid(),menu.getTitle(),open));
        }
        return new DataView(treeNodes);
    }
    //初始化排序码
    @PostMapping("/loadMenuMaxOrderNum")
    @ResponseBody
    public Map<String,Object> loadMenuMaxOrderNum(){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Menu>wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("ordernum");
        IPage<Menu>page = new Page<>(1,1);
        List<Menu>list = menuService.page(page,wrapper).getRecords();
        map.put("value",list.get(0).getOrdernum()+1);
        return map;
    }
    @PostMapping("/addMenu")
    @ResponseBody
    public DataView addMenu(Menu menu){
        DataView dataView = new DataView();
        menu.setType("menu");
        boolean save = menuService.save(menu);
        if (!save){
            dataView.setCode(100);
            dataView.setMsg("数据插入失败");
        }
        dataView.setCode(200);
        dataView.setMsg("菜单插入成功");
        return dataView;
    }
    @PostMapping("/updateMenu")
    @ResponseBody
    public DataView update(Menu menu){
        menuService.updateById(menu);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("更新菜单成功");
        return dataView;
    }
    @PostMapping("/checkMenuHasChildrenNode")
    @ResponseBody
    public Map<String,Object> checkMenuHasChildrenNode(Menu menu){

        Map<String,Object>map = new HashMap<>();

        QueryWrapper<Menu>wrapper = new QueryWrapper<>();
        wrapper.eq("pid",menu.getId());
        List<Menu> list = menuService.list(wrapper);
        if (list.size()>0){
            map.put("value",true);
        }else {
            map.put("value",false);
        }
        return map;
    }
    @PostMapping("/deleteMenu")
    @ResponseBody
    public DataView deleteMenu(Menu menu){

        menuService.removeById(menu.getId());
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除成功");
        return dataView;
    }

    @RequestMapping("/loadIndexLeftMenuJson")
    @ResponseBody
    public DataView loadIndexLeftMenuJson(Menu menu, HttpSession session){
        //查询所有菜单栏 按照条件查询【角色拥有的菜单】
        QueryWrapper<Menu>wrapper = new QueryWrapper<>();
        List<Menu>list = null;
        //取出session中的用户ID
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        //根据用户ID查询角色ID
        List<Integer> rids = roleService.queryUserRoleById(userId);
        //去重
        Set<Integer> mids = new HashSet<>();
        for (Integer rid : rids) {
            //根据角色ID查询菜单ID
            List<Integer> permissions = roleService.queryAllPermissionsByRid(rid);
            //菜单栏ID和角色ID去重
            mids.addAll(permissions);
        }
        //根据角色ID查询菜单ID
        if (mids.size()>0){
            wrapper.in("id",mids);
            list = menuService.list(wrapper);
        }

        List<TreeNode>treeNodes = new ArrayList<>();
        for (Menu m:list){
            Integer id = m.getId();
            Integer pid = m.getPid();
            String title = m.getTitle();
            String icon = m.getIcon();
            String href = m.getHref();
            Boolean open = m.getOpen().equals(1)?true:false;
            treeNodes.add(new TreeNode(id,pid,title,icon,href,open));

        }
        //层级关系
        List<TreeNode> nodeList = TreeNodeBuilder.build(treeNodes, 0);
        return new DataView(nodeList);
    }
}
